
package com.jf.cloud.auth.feign;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.api.auth.feign.TokenFeignClient;
import com.jf.cloud.auth.manager.TokenStore;
import com.jf.cloud.auth.model.AuthAccount;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.Hidden;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zz
 * @date 2020/7/15
 */
@RestController
@Hidden
public class TokenFeignController implements TokenFeignClient {

    private static final Logger logger = LoggerFactory.getLogger(TokenFeignController.class);

    @Autowired
    private TokenStore tokenStore;

    @Override
    public ServerResponseEntity<UserInfoInTokenBO> checkToken(String accessToken) {
        ServerResponseEntity<UserInfoInTokenBO> userInfoByAccessTokenResponse = tokenStore
                .getUserInfoByAccessToken(accessToken, true);
        if (!userInfoByAccessTokenResponse.isSuccess()) {
            return ServerResponseEntity.transform(userInfoByAccessTokenResponse);
        }
        return ServerResponseEntity.success(userInfoByAccessTokenResponse.getData());
    }

    @Override
    public ServerResponseEntity<Void> clearToken() {
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        tokenStore.deleteAllToken(userInfoInTokenBO.getSysType().toString(), userInfoInTokenBO.getUid());
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> deleteToken(Long userId, Integer sysType) {
        tokenStore.deleteAllToken(userId,sysType);
        return ServerResponseEntity.success();
    }
}
